package com.mapper;

import com.pojo.NoteTags;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NoteTagsMapper {

    @Insert("INSERT INTO note_tags(note_id, tag_id, created_at) " +
            "VALUES(#{noteId}, #{tagId}, #{createdAt})")
    int insert(NoteTags noteTag);

    @Delete("DELETE FROM note_tags WHERE note_id = #{noteId} AND tag_id = #{tagId}")
    int delete(@Param("noteId") Integer noteId, @Param("tagId") Integer tagId);

    @Delete("DELETE FROM note_tags WHERE note_id = #{noteId}")
    int deleteByNoteId(Integer noteId);

    @Delete("DELETE FROM note_tags WHERE tag_id = #{tagId}")
    int deleteByTagId(Integer tagId);

    @Select("SELECT * FROM note_tags WHERE note_id = #{noteId}")
    List<NoteTags> selectByNoteId(Integer noteId);

    @Select("SELECT * FROM note_tags WHERE tag_id = #{tagId}")
    List<NoteTags> selectByTagId(Integer tagId);

    @Select("SELECT COUNT(*) FROM note_tags WHERE note_id = #{noteId} AND tag_id = #{tagId}")
    int exists(@Param("noteId") Integer noteId, @Param("tagId") Integer tagId);

    @Select("SELECT tag_id FROM note_tags WHERE note_id = #{noteId}")
    List<Integer> selectTagIdsByNoteId(Integer noteId);

    @Select("SELECT note_id FROM note_tags WHERE tag_id = #{tagId}")
    List<Integer> selectNoteIdsByTagId(Integer tagId);
}